
<!-- 
@descrition: '' 
@auth: '' 
-->
<template>
	<view>
		<view class="input m-t-40">
			<u-field 
				label-width="0" 
				v-model="account" 
				:placeholder="loginType === 1 ? '请输入账号' : '请输入电话号码' " 
				:label-width="80"
				label-align="center"
				:border-bottom="false"
				:style="{ width: '100%' }" 
				type="number" >
				<template v-slot:icon>
				    <image v-show="loginType === 1" class="field-left-icon" src="/static/images/logo/icon11-18.png"></image>
					<image v-show="loginType === 2" class="field-left-icon" src="/static/images/logo/sj3.png"></image>
				</template>
			</u-field>
			
		</view>
		
		<view class="input m-t-40">
			<u-field
				label-width="0" 
				v-model="password" 
				:label-width="80"
				label-align="center"
				:placeholder="loginType === 1 ? '请输入密码' : '请输入验证码' " 
				:border-bottom="false"
				:password="!passwordShow"
				:style="{ width: '100%' }" 
				:type="loginType === 1 ? 'text' : 'number' " >
				<template v-slot:icon>
				    <image v-show="loginType === 1" class="field-left-icon" src="/static/images/logo/mm2.png"></image>
					<image v-show="loginType === 2" class="field-left-icon" src="/static/images/logo/yzm3.png"></image>
				</template>
				<template v-if="loginType === 1" slot="right"  >
					<view>
						<u-icon name="eye" @click="passwordShow = !passwordShow" v-show="!passwordShow"
						    size="36rpx">
						</u-icon>
						<u-icon name="eye-off" @click="passwordShow = !passwordShow" v-show="passwordShow"
						    size="36rpx"></u-icon>
					</view>
				    
				</template>
				<template  v-if="loginType === 2" v-slot:right>
					<view 
						class="code" 
						:class="{
						  disabled: codeTips == '获取验证码' && account.length !== 11,
						}"
						@tap="sendSMS"
					>
						
						{{ codeTips }}
					</view>
				</template>
			</u-field>
		</view>
		<u-verification-code unique-key="login" ref="uCode" @change="codeChange">
		</u-verification-code>
		<view class="input m-t-40">
			<button class=" login-btn login-btn-user" style="background-color: #ff0000;" @tap="loginFun" :class="{ disabled: isDisabled }">
				登 录
			</button>
			
		</view>
	
		
	</view>
</template>

<script>
/**
	 * @descrition: '' 
	 * @auth: '' 
	 */
	import {
		apiAuthLogin,
		apiAccountLogin,
		apiLoginCaptcha,
		apiToutiaoAuthLogin,
		apiAppWxLogin,
		apiUpdateUser,
		apiCheckMobile,
	} from "@/api/app";
	export default {
		props: {
			loginType: {
				type: Number,
				default: 1
			},
			isAgree: {
				type: Boolean,
				default: false
			}
			
		},
		data() {
			return {
				account: '',
				password: '',
				codeTips: '',
				passwordShow: false
			}
		},
		computed: {
			//登录按钮禁用样式
			isDisabled() {
				return this.account.length !== 11 || !this.password
				
			},
		},
		watch:{},
		created() {},
		mounted() {
			
			let NODE_ENV = ''
			try{
				NODE_ENV = process.env.NODE_ENV
			}catch(e){
				NODE_ENV= ''
				//TODO handle the exception
			}
			
			const password = NODE_ENV === "development" ? 'ces123456' : this.register.password
			this.password = this.loginType === 1 ? password : ''
			this.passwordShow = NODE_ENV === "development" ? true : false
		},

		methods: {
			codeChange(tip) {
				this.codeTips = tip;
			},
			loginFun() {
				
				const {
					account,
					password,
				} = this;
				if (!account) {
					this.$toast({
						title: "请输入手机号",
					});
					return;
				}
				
			
				if (this.loginType == 1 && !password) {
					this.$toast({
						title: "请输入密码",
					});
					return;
				}
				if (this.loginType == 2 && !password) {
					this.$toast({
						title: "请输入验证码",
					});
					return;
				}
				if (!this.isAgree) {
					this.$emit('agree')
					return;
				}
				const config = Object.assign({}, {
					account: account
				})
				if (this.loginType === 1) {
					Object.assign(config, {
						password: password,
						code: ''
					})
					
				} else {
					Object.assign(config, {
						password: '',
						code:password
					})
				}
				console.log("登录验证通过", config)
				// 这里只表示验证成功 
				return this.$emit("success", config)
			},
			// 发送验证码
			async sendSMS() {
				if (!this.$refs.uCode.canGetCode) return;
				if (!this.account) {
					this.$toast({
						title: "请输入手机号",
					});
					return;
				}
				try {
					/**
					 * @descriptionp 检测手机号是否已经注册
					 */
					const { has } = await apiCheckMobile({
						mobile: this.account
					});
					if (has === 0) {
						return this.$toast({
							title: "手机号未注册",
						});
					}
					apiLoginCaptcha({
							mobile: this.account,
						})
						.then((res) => {
							this.$refs.uCode.start();
						})
						.catch((err) => {
							console.log(err);
							console.log('发送验证码 接口报错', err);
						});
				} catch (error) {
					console.log('发送验证码 try catch 报错', error);
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	$input-h: 100rpx;
	.input {
		height: $input-h;
		border-radius: 999px;
		display: flex;
		align-items: center;
		background: #ffffff;
		overflow: hidden;
	}
	.field-left-icon {
	    width: 30rpx;
	    height: 30rpx;
	}
	.login-btn {
		width: 100%;
		height: 100%;
		line-height: $input-h;
		font-size: 38rpx;
		color: #fff;
		background-color:#ff8200;
	}
	.code {
		min-width: 200rpx;
		height: 84rpx;
		padding: 0 30rpx;
		line-height: 84rpx;
		background: #ff8200;
		border-radius: 999rpx;
		color: #fff;
		text-align: center;
		
	}
	.disabled {
	    opacity: 0.5;
	}
</style>